package com.codeoss.framework.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.request.AdvanceQueryPageRequest;
import com.codeoss.framework.request.ViewRequest;
import com.codeoss.framework.response.AdvanceQueryPageResponse;
import com.codeoss.framework.response.ViewResponse;
import com.codeoss.framework.service.IAdvanceQueryPageService;
import com.codeoss.framework.service.IViewService;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.store.IComponentStore;
import com.codeoss.framework.view.pagebean.ListViewPageBean;
import com.codeoss.framework.view.renderer.IComponentRenderer;
import com.codeoss.framework.view.renderer.ViewPropMap;
import com.codeoss.framework.view.renderer.store.IComponentRendererStore;

@Service
public class AdvanceQueryPageService implements IAdvanceQueryPageService {
    @Autowired
    private IViewService viewService;
    @Autowired
    private ITableStore tableStore;
    @Autowired
    private IComponentStore componentStore;
    @Autowired
    private IConverterStore converterStore;
    @Autowired
    private ListViewPageBean listViewPageBean;
    @Autowired
    private IComponentRendererStore componentRendererStore;
	@Override
	public AdvanceQueryPageResponse init(String code) {
		// TODO Auto-generated method stub
		AdvanceQueryPageResponse advanceQueryPageResponse=new AdvanceQueryPageResponse();
		YunContext yunContext=YunContext.getCurrentContext();
		yunContext.setQueryMap(new HashMap<>());
		ViewResponse viewResponse=viewService.initPage(yunContext,code);
		
		advanceQueryPageResponse.setViewSerializeMap(viewResponse.getViewSerializeMap());
		advanceQueryPageResponse.setViewPropMap(viewResponse.getViewPropMap());
		return advanceQueryPageResponse;
	}

	@Override
	public AdvanceQueryPageResponse dataSearch(String code,String listViewPageCode, AdvanceQueryPageRequest request) {
		// TODO Auto-generated method stub
		AdvanceQueryPageResponse advanceQueryPageResponse=new AdvanceQueryPageResponse();
		Page page=componentStore.get(listViewPageCode);
		Table table=tableStore.get(page.getDataSourceConfigList().get(0).getTableName());
		Query query=new Query();
		List<Map<String,String>> rowObjectArray=request.getRowObjectArray();
		for(int i=0;i<rowObjectArray.size();i++) {
			Map<String,String> map=rowObjectArray.get(i);
			String field=map.get("field");
			Column column=table.getColumnByColumnName(field);
			String sValue1=map.get("value1");
			String sValue2=map.get("value2");
			Object value1=null;
			Object value2=null;
			IConverter converter=converterStore.getConverter(column.getDataType());
			if(!StringUtils.isEmpty(sValue1)) {
				value1=converter.convertStringToObject(sValue1);
			}
			if(!StringUtils.isEmpty(sValue2)) {
				value2=converter.convertStringToObject(sValue2);
			}
			String op=map.get("op");
			if ("like".equals(op)) {
				query.add(Restrictions.like(column.getColumnName(),(String)value1));
			} else if ("between".equals(op)) {
				query.add(Restrictions.between(column.getColumnName(), value1, value2));
			} else if ("=".equals(op)) {
				query.add(Restrictions.eq(column.getColumnName(),value1));
			} else if ("<>".equals(op)) {
				query.add(Restrictions.ne(column.getColumnName(),value1));
			} else if (">".equals(op)) {
				query.add(Restrictions.gt(column.getColumnName(),value1));
			} else if ("<".equals(op)) {
				query.add(Restrictions.lt(column.getColumnName(),value1));
			} else if (">=".equals(op)) {
				query.add(Restrictions.ge(column.getColumnName(),value1));
			} else if ("<=".equals(op)) {
				query.add(Restrictions.le(column.getColumnName(),value1));
			} else if ("isnull".equals(op)) {
				query.add(Restrictions.isNull(column.getColumnName()));
			} else if ("isnotnull".equals(op)) {
				query.add(Restrictions.isNotNull(column.getColumnName()));
			}
		}
		YunContext yunContext=YunContext.getCurrentContext();
		ViewRequest viewRequest=new ViewRequest();
		viewRequest.setData(new HashMap<String,String>());
		yunContext.setViewRequest(viewRequest);
		yunContext.setPage(page);
		listViewPageBean.dataSearch(true, query);
		
		IComponentRenderer componentRenderer=componentRendererStore.getComponentRenderer(yunContext,page);
		ViewPropMap viewPropMap=new ViewPropMap();
		componentRenderer.renderProp(yunContext, page, viewPropMap);
		advanceQueryPageResponse.setViewPropMap(viewPropMap);
		return advanceQueryPageResponse;
	}

}
